home *** CD-ROM | disk | FTP | other *** search
/ Corel Draw 9 / CorelDraw9 Disco 1.iso / Scripts / cdplayer.csc < prev    next >
Text File  |  1998-07-30  |  11KB  |  377 lines

  1. REM Ejecuta CDs de sonido
  2. REM CDPlayer.csc 22 de agosto de 1996
  3. REM Copyright 1996 Corel Corporation. Reservados todos los derechos.
  4.  
  5. ' Constants
  6. GLOBAL CONST STYLE_MINIMIZEBOX% = &h0020
  7. GLOBAL CONST EVENT_CHANGE% = 1
  8. GLOBAL CONST EVENT_MOUSE_CLICK% = 2
  9. GLOBAL CONST EVENT_DBL_MOUSE_CLICK% = 3
  10.  
  11. 'Declare Multimedia DLL function
  12. DECLARE FUNCTION MMString LIB "winmm" (BYVAL Command AS STRING, BYVAL ReturnString AS STRING, BYVAL ReturnSize AS LONG, BYVAL CallBack AS LONG) AS LONG ALIAS "mciSendStringA"
  13.  
  14. 'Global procedures
  15. DECLARE FUNCTION MediaPresent() AS BOOLEAN
  16. DECLARE SUB EndProgram()
  17. DECLARE FUNCTION CDCommand$( Command$, ErrorString$ )
  18.  
  19. ' Global variables
  20. GLOBAL NumberOfTracks%
  21. GLOBAL PauseMode AS BOOLEAN
  22. GLOBAL PausePosition$ AS STRING
  23.  
  24. ' Dialog definition
  25. BEGIN DIALOG OBJECT CDDialog 108, 127, "Corel Script CD", SUB CDDialogSub
  26.     PUSHBUTTON  4, 5, 53, 16, .PlayButton, ">"
  27.     PUSHBUTTON  59, 5, 16, 16, .PauseButton, "II"
  28.     PUSHBUTTON  22, 23, 16, 16, .StopButton, "O"
  29.     PUSHBUTTON  4, 23, 16, 16, .PrevTrack, "|<<"
  30.     PUSHBUTTON  41, 23, 16, 16, .NextTrack, ">>|"
  31.     PUSHBUTTON  59, 23, 16, 16, .Eject, "^"
  32.     VSLIDER 89, 0, 19, 118, .Slider
  33.     GROUPBOX  5, 42, 81, 72, .Trackbox, "Track"
  34.     PUSHBUTTON  10, 52, 14, 14, .T1, "1"
  35.     PUSHBUTTON  24, 52, 14, 14, .T2, "2"
  36.     PUSHBUTTON  38, 52, 14, 14, .T3, "3"
  37.     PUSHBUTTON  52, 52, 14, 14, .T4, "4"
  38.     PUSHBUTTON  66, 52, 14, 14, .T5, "5"
  39.     PUSHBUTTON  10, 66, 14, 14, .T6, "6"
  40.     PUSHBUTTON  24, 66, 14, 14, .T7, "7"
  41.     PUSHBUTTON  38, 66, 14, 14, .T8, "8"
  42.     PUSHBUTTON  52, 66, 14, 14, .T9, "9"
  43.     PUSHBUTTON  66, 66, 14, 14, .T10, "10"
  44.     PUSHBUTTON  10, 80, 14, 14, .T11, "11"
  45.     PUSHBUTTON  24, 80, 14, 14, .T12, "12"
  46.     PUSHBUTTON  38, 80, 14, 14, .T13, "13"
  47.     PUSHBUTTON  52, 80, 14, 14, .T14, "14"
  48.     PUSHBUTTON  66, 80, 14, 14, .T15, "15"
  49.     PUSHBUTTON  10, 94, 14, 14, .T16, "16"
  50.     PUSHBUTTON  24, 94, 14, 14, .T17, "17"
  51.     PUSHBUTTON  38, 94, 14, 14, .T18, "18"
  52.     PUSHBUTTON  52, 94, 14, 14, .T19, "19"
  53.     PUSHBUTTON  66, 94, 14, 14, .T20, "20"
  54.     STATUS .Info
  55. END DIALOG
  56.  
  57. '#########################################################################
  58. 'Set defaults
  59. GLOBAL Command AS STRING
  60. GLOBAL ErrorMsg AS LONG
  61. GLOBAL ReturnVal AS STRING
  62. GLOBAL CurrentTrackLength AS INTEGER
  63. ON ERROR GOTO ScriptError
  64. WITH CDDialog
  65.     .SetStyle STYLE_MINIMIZEBOX
  66.     .SetTimer 1000
  67.     .Slider.SetMinRange 0
  68.     .Slider.SetMaxRange 1
  69.     .Slider.SetTick 60
  70. END WITH
  71. NumberOfTracks% = 0
  72.  
  73. 'Reset device
  74. Command$ = "close cdaudio"
  75. ReturnVal$ = SPACE(300)
  76. ErrorMsg& = MMString(Command$,ReturnVal$,255&,0&)
  77. ' Open device
  78. CDCommand "open cdaudio", " initializing CD!"
  79. 'Initialize time format
  80. CDCommand "set cdaudio time format tmsf wait", " initializing CD!"
  81.  
  82. '#########################################################################
  83. 'Call Main Routine
  84. DIALOG CDDialog
  85.  
  86. '#########################################################################
  87. 'End Script
  88. ScriptEnd:
  89.     'Stop the CD
  90.     IF MediaPresent() THEN
  91.         PauseMode = FALSE
  92.         Command$ = "stop cdaudio"
  93.         ReturnVal$ = SPACE(300)
  94.         ErrorMsg& = MMString(Command$,ReturnVal$,255&,0&)
  95.     ENDIF
  96.     'Close the device
  97.     Command$ = "close cdaudio"
  98.     ReturnVal$ = SPACE(300)
  99.     ErrorMsg& = MMString(Command$,ReturnVal$,255&,0&)
  100.     END
  101.  
  102. '#########################################################################
  103. ' Error handling    
  104. ScriptError:
  105.     ' We try to close the device
  106.     RESUME AT ScriptEnd    
  107.  
  108. '#########################################################################
  109. ' Procedures
  110.  
  111. ' Send a command to the CD player
  112. FUNCTION CDCommand$( Command$, ErrorString$ )
  113.     CDCommand$ = SPACE(300)
  114.     ErrorMsg& = MMString(Command$,CDCommand$,255&,0&)
  115.     IF ErrorMsg& <> 0 THEN
  116.         MESSAGE "Error " & ErrorMsg& & ErrorString$
  117.         FAIL 800
  118.     ENDIF
  119. END FUNCTION
  120.  
  121. ' Test presence of CD in drive
  122. FUNCTION MediaPresent() AS BOOLEAN
  123.     'Check for a CD
  124.     Command$ = "status cdaudio media present"
  125.     ReturnVal$ = SPACE(300)
  126.     ErrorMsg& = MMString(Command$,ReturnVal$,255&,0&)
  127.     IF ErrorMsg& <> 0 THEN MediaPresent = FALSE
  128.     MediaPresent = CBOL(ReturnVal$)
  129. END FUNCTION
  130.  
  131. ' Get current track
  132. FUNCTION CDGetTrack%()
  133.     IF MediaPresent() THEN
  134.         'Gets the current track
  135.         CDGetTrack% = CDCommand("status cdaudio current track", " reading CD!")
  136.     ELSE
  137.         CDGetTrack%=1
  138.     END IF
  139. END FUNCTION
  140.  
  141. ' Play requested Track
  142. SUB CDPlay(BYVAL TrackToPlay%)
  143.     IF MediaPresent() THEN
  144.         'Remove pause
  145.         PauseMode = FALSE
  146.         'Have the CD play the track
  147.         IF TrackToPlay%<1 THEN TrackToPlay%=1
  148.         IF TrackToPlay%<=NumberOfTracks% THEN 
  149.             CDCommand "play cdaudio from " & TrackToPlay%, " playing CD!"
  150.             ' GetTrack Length
  151.             ReturnVal$ = CDCommand("status cdaudio length track " & TrackToPlay%, " getting status!")
  152.             CurrentTrackLength%=CINT(LEFT(ReturnVal$,2))*60+CINT(MID(ReturnVal$,4,2))
  153.         END IF
  154.     ENDIF
  155. END SUB
  156.  
  157. ' Seek to requested position on current track
  158. SUB CDSeek(BYVAL SeekPos%)
  159.     DIM Min AS INTEGER
  160.     DIM Sec AS INTEGER
  161.  
  162.     IF MediaPresent() THEN
  163.         'Remove pause
  164.         PauseMode = FALSE
  165.         ' Get track info
  166.         Min%=SeekPos%\60
  167.         SeekPos%=SeekPos%-Min%*60
  168.         Sec%=SeekPos%
  169.         'Have the CD play the track
  170.         CDCommand "play cdaudio from " & CDGetTrack%() & ":" & Min% & ":" & Sec%, " playing CD!"
  171.     ENDIF
  172. END SUB
  173.  
  174. ' Stop CD
  175. SUB CDStop()
  176.     IF MediaPresent() THEN
  177.         'Remove pause
  178.         PauseMode = FALSE
  179.         'Have the CD stop playing
  180.         CDCommand "stop cdaudio", " stopping CD!"
  181.     END IF
  182. END SUB
  183.  
  184. ' Eject CD
  185. SUB CDEject()
  186.     PauseMode = FALSE
  187.     CDCommand "set cdaudio door open", " ejecting CD!"
  188. END SUB
  189.  
  190. ' Pause/Unpause CD
  191. SUB CDPause()
  192.     IF MediaPresent() THEN
  193.         IF PauseMode THEN
  194.             'We are paused; resume
  195.             PauseMode = FALSE
  196.             'Play the CD from the position saved when paused.
  197.             CDCommand "play cdaudio from " & PausePosition$, " resuming CD!"
  198.         ELSE
  199.             'We need to set a pause
  200.             PauseMode = TRUE
  201.             'Get position and keep it
  202.             PausePosition$ = CDCommand("status cdaudio position", " pausing CD!")
  203.             'Stop the CD from playing
  204.             CDCommand "stop cdaudio", " stopping CD!"
  205.         ENDIF
  206.     ENDIF
  207. END SUB
  208.  
  209. 'Update the dialog display
  210. SUB UpdateDisplay()
  211.     DIM NewNumberOfTracks AS INTEGER
  212.     DIM Track AS STRING
  213.     DIM Sec AS INTEGER
  214.  
  215.     'Initialize number of tracks
  216.     IF MediaPresent() THEN
  217.         NewNumberOfTracks% = CDCommand("status cdaudio number of tracks", " getting track info!")
  218.     ELSE
  219.         NewNumberOfTracks% = 0
  220.     ENDIF
  221.     
  222.     WITH CDDialog
  223.         ' Update track buttons
  224.         IF NewNumberOfTracks%<>NumberOfTracks% THEN
  225.                 NumberOfTracks%=NewNumberOfTracks%
  226.                 .T1.Enable NumberOfTracks%>0 
  227.                 .T2.Enable NumberOfTracks%>1 
  228.                 .T3.Enable NumberOfTracks%>2 
  229.                 .T4.Enable NumberOfTracks%>3 
  230.                 .T5.Enable NumberOfTracks%>4 
  231.                 .T6.Enable NumberOfTracks%>5 
  232.                 .T7.Enable NumberOfTracks%>6 
  233.                 .T8.Enable NumberOfTracks%>7 
  234.                 .T9.Enable NumberOfTracks%>8 
  235.                 .T10.Enable NumberOfTracks%>9 
  236.                 .T11.Enable NumberOfTracks%>10 
  237.                 .T12.Enable NumberOfTracks%>11 
  238.                 .T13.Enable NumberOfTracks%>12 
  239.                 .T14.Enable NumberOfTracks%>13 
  240.                 .T15.Enable NumberOfTracks%>14 
  241.                 .T16.Enable NumberOfTracks%>15 
  242.                 .T17.Enable NumberOfTracks%>16 
  243.                 .T18.Enable NumberOfTracks%>17 
  244.                 .T19.Enable NumberOfTracks%>18 
  245.                 .T20.Enable NumberOfTracks%>19 
  246.         ENDIF                
  247.  
  248.         'Get status and update
  249.         ReturnVal$ = CDCommand("status cdaudio mode", " reading position!")
  250.         SELECT CASE ReturnVal$
  251.             CASE "stopped"
  252.                 IF PauseMode THEN
  253.                     'We're paused, indicate where
  254.                     .Info.SetText "Paused-" & "Track " & CINT(LEFT(PausePosition$,2)) & " Time:" & MID(PausePosition$,4,5)
  255.                     .PlayButton.Enable TRUE
  256.                     .StopButton.Enable TRUE
  257.                     .PauseButton.Enable TRUE
  258.                     .NextTrack.Enable TRUE
  259.                     .PrevTrack.Enable TRUE
  260.                     .Eject.Enable TRUE
  261.                     .Slider.Enable TRUE
  262.                 ELSE
  263.                     'We're really stopped
  264.                     .Info.SetText "Stopped"
  265.                     .PlayButton.Enable TRUE
  266.                     .Slider.Enable TRUE
  267.                     .StopButton.Enable FALSE
  268.                     .PauseButton.Enable FALSE
  269.                     .NextTrack.Enable FALSE
  270.                     .PrevTrack.Enable FALSE
  271.                     .Eject.Enable TRUE
  272.                 ENDIF
  273.     
  274.             CASE "not ready"
  275.                 'Device not ready; don't allow anything
  276.                 .Info.SetText "Not ready"
  277.                 .PlayButton.Enable FALSE
  278.                 .Slider.Enable FALSE
  279.                 .StopButton.Enable FALSE
  280.                 .PauseButton.Enable FALSE
  281.                 .NextTrack.Enable FALSE
  282.                 .PrevTrack.Enable FALSE
  283.                 .Eject.Enable FALSE
  284.     
  285.             CASE "open"
  286.                 'No CD or door is open; don't allow anything
  287.                 .Info.SetText "No CD"
  288.                 .PlayButton.Enable FALSE
  289.                 .Slider.Enable FALSE
  290.                 .StopButton.Enable FALSE
  291.                 .PauseButton.Enable FALSE
  292.                 .NextTrack.Enable FALSE
  293.                 .PrevTrack.Enable FALSE
  294.                 .Eject.Enable FALSE
  295.     
  296.             CASE "playing"
  297.                 'Playing; get position and indicate it
  298.                 ReturnVal$ = CDCommand("status cdaudio position", " reading position!")
  299.                 Track$=CINT(LEFT(ReturnVal$,2))
  300.                 .Info.SetText "Playing-" & "Track " & Track$ & " Time:" & MID(ReturnVal$,4,5)
  301.                 .PlayButton.Enable TRUE
  302.                 .Slider.Enable TRUE
  303.                 .StopButton.Enable TRUE
  304.                 .PauseButton.Enable TRUE
  305.                 .NextTrack.Enable TRUE
  306.                 .PrevTrack.Enable TRUE
  307.                 .Eject.Enable TRUE
  308.                 ' Update slider range if track has changed
  309.                 STATIC SliderTrack$
  310.                 IF SliderTrack$<>Track$ THEN
  311.                     SliderTrack$=Track$
  312.                     ReturnVal$ = CDCommand("status cdaudio length track " & Track$, " getting status!")
  313.                     CurrentTrackLength%=CINT(LEFT(ReturnVal$,2))*60+CINT(MID(ReturnVal$,4,2))
  314.                     CDDialog.Slider.SetMaxRange CurrentTrackLength%
  315.                     .Slider.SetValue 0
  316.                 ELSE
  317.                     ' Update slider
  318.                     Sec%=CINT(MID(ReturnVal$,4,2))*60+CINT(MID(ReturnVal$,7,2))
  319.                     .Slider.SetValue Sec%
  320.                 END IF
  321.  
  322.             CASE ELSE
  323.                 'Other (unknown); assume not ready.
  324.                 .Info.SetText "Not ready"
  325.                 .PlayButton.Enable FALSE
  326.                 .Slider.Enable FALSE
  327.                 .StopButton.Enable FALSE
  328.                 .PauseButton.Enable FALSE
  329.                 .NextTrack.Enable FALSE
  330.                 .PrevTrack.Enable FALSE
  331.                 .Eject.Enable FALSE
  332.         END SELECT
  333.     END WITH
  334. END SUB
  335.  
  336. '#########################################################################
  337. '# CDPlayDialogSub: Main Dialog Event Handler, the heart of    *
  338. '# the program.                                                *
  339. '#########################################################################
  340. SUB CDDialogSub (BYVAL ControlID%, BYVAL Event%)
  341.     WITH CDDialog
  342.       IF Event% = EVENT_CHANGE AND ControlID% = .Slider.GetID() THEN
  343.                 CDSeek(.Slider.GetValue()) 
  344.         ELSEIF Event% = EVENT_MOUSE_CLICK THEN
  345.             SELECT CASE ControlID%
  346.                 CASE .PlayButton.GetID()
  347.                     CDPlay CDGetTrack%()     'Play the Current track
  348.                 CASE .StopButton.GetID()
  349.                     CDStop     'Stops the CD from playing
  350.                 CASE .Eject.GetID()
  351.                     CDEject    'Ejects the CD
  352.                 CASE .PauseButton.GetID()
  353.                     CDPause 'Pauses or Resumes playing
  354.                 CASE .PrevTrack.GetID()
  355.                     'Goes back one track from the one that is played, and plays
  356.                     'that track.
  357.                     CDPlay(CDGetTrack%()-1)
  358.                 CASE .NextTrack.GetID()
  359.                     'Goes ahead one track from the one that is played, and plays
  360.                     'that track.
  361.                     CDPlay(CDGetTrack%()+1)
  362.                 CASE .Slider.GetID()
  363.                     'Set new position
  364.                     CDSeek(.Slider.GetValue())
  365.                 CASE ELSE
  366.                     'Did we pressed a track button
  367.                     IF ControlID%>=.T1.GetID() AND ControlID%<=.T20.GetID() THEN
  368.                         CDPlay(ControlID%-.T1.GetID()+1)
  369.                     END IF
  370.                     
  371.             END SELECT
  372.         END IF
  373.         ' Update dialog display
  374.         UpdateDisplay
  375.     END WITH
  376. END SUB
  377.